package com.echoesnet.echoesshortvideo.views.adapters;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTransaction;

import com.echoesnet.echoesshortvideo.Beans.Video;
import com.echoesnet.echoesshortvideo.Fragments.VideoPlayFragment;
import com.echoesnet.echoesshortvideo.views.VerticalViewPager;
import com.orhanobut.logger.Logger;

import java.util.List;

/**
 * @author Administrator
 * @Date 2018/4/27
 * @Version 1.0
 */
public class VerticalViewPagerAdapter extends FragmentStatePagerAdapter// FragmentPagerAdapter//FragmentStatePagerAdapter//
{
    private final String TAG = VerticalViewPagerAdapter.class.getSimpleName();

    private FragmentManager mFragmentManager;
    private List<Video> mPagerItems;

    private VerticalViewPager verticalViewPager;
    private int videoIndex;//点击的视频在列表中的位置
    private Activity mActivity;
    private String openSource;

    public VerticalViewPagerAdapter(Activity activity, int index, FragmentManager fm, List<Video> listData, VerticalViewPager verticalViewPager, String openSource)
    {
        super(fm);
        mActivity = activity;
        videoIndex = index;
        mFragmentManager = fm;
        mPagerItems = listData;
        this.verticalViewPager = verticalViewPager;
        this.openSource = openSource;
    }

    @Override
    public Fragment getItem(int i)
    {
        Logger.t(TAG).d("------->aaaa getItem():" + i + "mPagerItems.size: " + mPagerItems.size());

        VideoPlayFragment videoPlayFragment = new VideoPlayFragment();
        Video video = mPagerItems.get(i);
        videoPlayFragment.setTitle(video.getVideoId());

        Bundle videoBundle = new Bundle();
        videoBundle.putParcelable("videoDetail", video);
        if (videoIndex == i)//给将要自动播放的fragment设置标志
        {
            videoBundle.putBoolean("autoPlay", true);
        }
        videoPlayFragment.setArguments(videoBundle);

        videoPlayFragment.setOnVideoDeleteListener(videoInfo ->
        {
//            int deleteIndex = 0;
//            for (int j = 0; j < mPagerItems.size(); j++)
//            {
//                if (videoInfo.getVideoId().equals(mPagerItems.get(j).getVideoId()))
//                {
//                    deleteIndex = j;
//                    break;
//                }
//            }
//            deleteItem(deleteIndex);
            mPagerItems.remove(videoInfo);
            if (mPagerItems.size() == 0)
                mActivity.finish();
            if ("1".equals(openSource))//从个人详情页进入，删除视频，关闭此页   ---yqh
                mActivity.finish();
            notifyDataSetChanged();
        });
        videoPlayFragment.setParentVerticalViewPager(verticalViewPager);


        return videoPlayFragment;
    }


    private IOnVideoDeleteListener deleteListener;

    public interface IOnVideoDeleteListener
    {
        void onDelete(Video video);
    }

    public void setOnVideoDeleteListener(IOnVideoDeleteListener listener)
    {
        this.deleteListener = listener;
    }

    @Override
    public int getCount()
    {
        return mPagerItems.size();
    }

    @Override
    public int getItemPosition(@NonNull Object object)
    {
        VideoPlayFragment fragment = (VideoPlayFragment) object;
        String videoId = fragment.getTitle();
        int position = -1;
        for (int i = 0; i < mPagerItems.size(); i++)
        {
            if (mPagerItems.get(i).getVideoId().equals(videoId))
            {
                position = i;
                break;
            }
        }
        Logger.t(TAG).d("------->aaaa getItem():" + position + "mPagerItems.size: " + mPagerItems.size());
        if (position >= 0)
        {
            return position;
        }
        else
        {
            Logger.t(TAG).d("------->aaaa 返回position NONE");
            return POSITION_NONE;
        }
    }

    public void deleteItem(int deletePosition)
    {
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        List<Fragment> fragments = mFragmentManager.getFragments();

        Fragment fragment = fragments.get(deletePosition);
        mPagerItems.remove(deletePosition);
        transaction.remove(fragment);
        notifyDataSetChanged();
    }

}
